package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.product.dto.ProductCategoryFormDTO;
import com.bfly.mall.product.entity.ProductCategory;
import com.bfly.mall.product.service.IProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 11:33
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategory, Integer> implements IProductCategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ProductCategoryFormDTO formDTO) {
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(formDTO, category);

        category.setCreateDate(new Date());
        category.setUpdateDate(category.getCreateDate());
        super.save(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ProductCategoryFormDTO formDTO) {
        ProductCategory category = get(formDTO.getId());
        if (category == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的产品类目!");
        }
        category.setName(formDTO.getName());
        category.setPic(formDTO.getPic());
        category.setSeq(formDTO.getSeq());

        category.setUpdateDate(new Date());
        super.edit(category);
    }
}
